/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import unc.pds.data.Consts;
import unc.pds.model.DiaryRemote;
import unc.pds.model.Postable;
import unc.pds.model.UserRemote;
import unc.pds.service.ServiceBeanRemote;
import unc.pds.service.ServiceBeanRemoteHome;
import unc.pds.service.UserHolder;
import unc.pds.util.Arch;
import unc.pds.util.ArchImpl;

/**
 *
 * @author Михаил
 */
public class PostServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ServiceBeanRemote service = ((ServiceBeanRemoteHome)(new InitialContext()).lookup("ejb/ServiceBean")).create((Long)request.getSession().getAttribute("sid"));
            if (request.getParameter("id") != null) {
                DiaryRemote diary = (DiaryRemote) service.getByKey(new Long(Long.parseLong(request.getParameter("id"))));
                diary.setCaption(request.getParameter("title"));
                diary.setDiary(request.getParameter("content"));
                response.sendRedirect("index.jsp?id=" + diary.getDiary());
            } else {
                Arch doc = new ArchImpl("root", null);
                Postable user = (Postable) service.getByKey(new Long(Long.parseLong(request.getParameter("userid"))));
                doc = new ArchImpl("root", null);
                doc.addChild(new ArchImpl(Consts.ATTR__DIARY_CAPTION, request.getParameter("title")));
                doc.addChild(new ArchImpl(Consts.CONT__DIARY_POST, request.getParameter("content")));
                doc.addChild(new ArchImpl(Consts.ATTR__DIARY_AUTHOR, service.getUser().getKey().toString()));
                user.addDiary(doc);
                response.sendRedirect("index.jsp?id=" + user.getKey() + "&view=posts");
            }
        } catch (Exception e) {
            out.println(e.getLocalizedMessage());
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
